/**
 * 分类书籍
 * @author Qasim <15750783791@163.com>
 * @version 2016/10/31
 */
document.addEventListener('plusready' ,ready , false);

var self = memberInfo = ''
function ready(){ 
	self = plus.webview.currentWebview();
	memberInfo = plus.storage.getItem('memberInfo');
	memberInfo = $.parseJSON(memberInfo);  
	myGoodscart(memberInfo.aid);
	var data = plus.storage.getItem('index_info');
	data = $.parseJSON(data);
	$.each(data.new, function(k ,v) {
		var html  = '<div class="list-p"><ul class="mui-table-view">';
				html += '<li class="mui-table-view-cell mui-media"><a href="javascript:;">';
				html += '<img class="mui-media-object mui-pull-left" src="'+getImgUrl(v.litpic , "../public/img/book.jpg")+'">';
				html += '<div class="mui-media-body"><div class="details" data-id="'+v.id+'" data-price="'+v.price+'"><span class="mui-h5 back"><span class="book_name">'+v.book_name+'</span><i class="mui-icon iconfont mui-red">&#xe6e4;</i></span>';
			    html += '<p class="mui-pull-right"><span class="mui-red">'+(parseInt(v.price) == 0 ? '免费' : v.price+' 元')+'</span></p>';
				html += '<p class="mui-h6 detail">'+v.description+'</p></div>'; 
				html += '<p class="autor"><span class="mui-h6 back">'+v.author+'</span>';
				if(parseInt(v.price) > 0){
					html += '<span class="mui-pull-right" ><button type="button" class="mui-btn">加入购物车</button></span>';
				}
				html += '</p></div></a></li></ul></div>';
		$('.lists').append(html);
		$('.details').unbind();
	    $('.details').on('click' , function(){ 
	    	bookDetails($(this).attr('data-id') , $(this).attr('data-price'))
	    });
	    
	    $('.list-p button').unbind();
	    $('.list-p button').on('click' , function(){
	    	var __this = $(this).parent().parent().parent();
	    	addGoodscart( 
	    		memberInfo.aid , 
	    		__this.find('.details').attr('data-id') ,
	    		__this.find('.book_name').text() ,  
	    		__this.find('.details').attr('data-price'),
	    		__this.find('.autor .back').text(),
	    		__this.parent().find('img').attr('src')
	    	);
	    });
	});

	//前往购物车
	$('.goodscart').on('click' , function(){
		var pamameter = {page : self.id}; 
		goUrl('shopping-car.html' , 'goodscart' ,  pamameter)
	})
}


/**
 * 分页加载数据
 * @param {Int} page [开始页] 
 */
function loadMore(page , isEnable){ 
	$.ajax({
		type:"post",  
		url:apiRoot + "/Home/Article/loadMore/", 
		data:{
			tid : 37,
			page :  page  
		},
		beforeSend:function(){ 
			plus.nativeUI.showWaiting('加载中...');
		},
		complete:function(){
			plus.nativeUI.closeWaiting();
		},
		dataType:'json', 
 		success : function(data){ 
			if(data != ''){
				$.each(data, function(k ,v) {
					var html  = '<div class="list-p"><ul class="mui-table-view">';
							html += '<li class="mui-table-view-cell mui-media"><a href="javascript:;">';
							html += '<img class="mui-media-object mui-pull-left" src="'+getImgUrl(v.litpic , "../public/img/book.jpg")+'">';
							html += '<div class="mui-media-body"><div class="details" data-id="'+v.id+'" data-price="'+v.price+'"><span class="mui-h5 back"><span class="book_name">'+v.book_name+'</span><i class="mui-icon iconfont mui-red">&#xe6e4;</i></span>';
						    html += '<p class="mui-pull-right"><span class="mui-red">'+(parseInt(v.price) == 0 ? '免费' : v.price+' 元')+'</span></p>';
							html += '<p class="mui-h6 detail">'+v.description+'</p></div>';  
							html += '<p class="autor"><span class="mui-h6 back">'+v.author+'</span>';
							if(parseInt(v.price) > 0){
								html += '<span class="mui-pull-right" ><button type="button" class="mui-btn">加入购物车</button></span>';
							}
							html += '</p></div></a></li></ul></div>';
					$('.lists').append(html);  
					$('.details').unbind();
				    $('.details').on('click' , function(){ 
				    	bookDetails($(this).attr('data-id') , $(this).attr('data-price'),self.id)
				    });
				    
				    $('.list-p button').unbind();
				    $('.list-p button').on('click' , function(){
				    	var __this = $(this).parent().parent().parent();
				    	addGoodscart( 
				    		memberInfo.aid , 
				    		__this.find('.details').attr('data-id') ,
				    		__this.find('.book_name').text() ,  
				    		__this.find('.details').attr('data-price'),
				    		__this.find('.autor .back').text(),
				    		__this.parent().find('img').attr('src')
				    	);
				    });  
				}); 
				if(isEnable)isEnable.endPullUpToRefresh(false); 
			}else{ 
				toast('没有更多的数据了');
				if(isEnable)isEnable.endPullUpToRefresh(true); 
			}
		},
		error : function(e){
			errorPrompt(e);
		}
	});
}


/**
 * 更新点击量
 */ 
window.addEventListener('update_goodscart' , function(result){  
	$('.total').text(parseInt($('.total').text()) - parseInt(result.detail.num));
})